package com.example.administrator.test;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31.
 */

public class ThreeFragment extends Fragment implements View.OnClickListener {

    ListView listView;
    List<Book> list = new ArrayList<>();
    Tab3ListAdapter adapter;

    private MyApplication app1;
    private MyApplication app2;
    DbManager db;

    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_three,null);

        app1 = new MyApplication();
        app2 = (MyApplication)getActivity().getApplication();
        intiData();

        listView = (ListView)view.findViewById(R.id.tab3_listView);
        adapter = new Tab3ListAdapter(getActivity(),list);
        listView.setAdapter(adapter);

        adapter.setOnButton(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent detail = new Intent(getActivity(),BookDetailActivity.class);
                detail.putExtra("book",list.get(i));
                startActivity(detail);
            }
        });

        return view;
    }

    private void intiData() {
         db= x.getDb(app1.getDaoConfig());

        try {
            Collect collects = db.selector(Collect.class).findFirst();
            if(collects==null){
                Toast.makeText(getActivity(),"没有任何收藏", Toast.LENGTH_SHORT).show();
                return;
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        try {
            List<Collect> collects = db.selector(Collect.class).where("account","==",app2.getAccount()).findAll();
            if(collects.size()!=0){
                for (int i=0;i<collects.size();i++){
                    Book find = db.selector(Book.class).where("id","==",collects.get(i).getBook_id()).findFirst();
                    if(find!=null){
                        list.add(find);
                    }
                }
            }else{
                Toast.makeText(getActivity(),"没有任何收藏", Toast.LENGTH_SHORT).show();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        Object tag = view.getTag();
        final int position = (Integer)tag;
        switch(view.getId())
        {
            case R.id.tab3_list_item_collect:
               AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
               builder.setIcon(R.mipmap.ic_launcher_round)
                       .setTitle("提示")
                       .setMessage("确定删除？")
                       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    Collect find_collect = db.selector(Collect.class).where("account","==",app2.getAccount()).and("book_id","==",list.get(position).getId()).findFirst();
                                    if(find_collect!=null){
                                        db.delete(find_collect);
                                        Toast.makeText(getActivity(),"取消收藏", Toast.LENGTH_SHORT).show();
                                        list.remove(position);
                                        adapter.notifyDataSetChanged();
                                    }else{
                                        Toast.makeText(getActivity(),"无数据取消", Toast.LENGTH_SHORT).show();
                                    }
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .create();             //创建AlertDialog对象
                builder.show();

                break;
        }
    }
}
